import { defineStore } from 'pinia';
import { store } from '@/store';
import { IParam } from '@/core';

interface ViewActionState {
	appViews: any[];
	viewSplit: IParam;
}

const useViewActionStore = defineStore({
	id: 'viewAction',
	state: (): ViewActionState => ({
		appViews: [],
		viewSplit: {},
	}),
	getters: {
		getStateByPath() {
			return (path: string): boolean => {
				const appView = this.appViews.find((appView: any) =>
					Object.is(appView.path, path)
				);
				if (appView) {
					return appView.viewDataChange;
				} else {
					return false;
				}
			};
		},
		getStateByViewTag() {
			return (viewTag: string): boolean => {
				const appView = this.appViews.find((appView: any) =>
					Object.is(appView.viewTag, viewTag)
				);
				if (appView) {
					return appView.viewDataChange;
				} else {
					return false;
				}
			};
		},
		getViewSplit() {
			return (tag: string): number | undefined => {
				if (this.viewSplit.hasOwnProperty(tag)) {
					return this.viewSplit[tag];
				}
				return undefined;
			};
		},
	},
	actions: {
		setViewSplit(tag: string, split: number) {
			this.viewSplit[tag] = split;
		},
	},
});

export function useViewActionStoreWithOut() {
	return useViewActionStore(store);
}
